package com.qzdsoft;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.web.servlet.ErrorPage;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import com.qzdsoft.erpcloud.interceptor.PermissionInterceptor;
import com.qzdsoft.erpcloud.interceptor.SessionExtendsInterceptor;
import com.qzdsoft.erpcloud.interceptor.TablePermissionInterceptor;
import com.qzdsoft.erpcloud.interceptor.UserLoginInterceptor;

@Configuration
@ComponentScan(basePackageClasses = App.class, useDefaultFilters = true)
public class WebAppConfig extends WebMvcConfigurerAdapter
{

    @Autowired
    private PermissionInterceptor permssionInterceptor;
    
    @Autowired
    private UserLoginInterceptor userLoginInterceptor;
    
    @Autowired
    private TablePermissionInterceptor tablePermissionInterceptor;
    
    @Autowired
    private SessionExtendsInterceptor sessionExtendsInterceptor;
    
    @Override
    public void addInterceptors(InterceptorRegistry registry)
    {
        
        registry.addInterceptor(userLoginInterceptor).addPathPatterns("/**")
            .excludePathPatterns("/login*").excludePathPatterns("/kaptch")
            .excludePathPatterns("/401").excludePathPatterns("/404").excludePathPatterns("/500").excludePathPatterns("/register*");
        
        registry.addInterceptor(sessionExtendsInterceptor).addPathPatterns("/**")
            .excludePathPatterns("/login*").excludePathPatterns("/kaptch")
            .excludePathPatterns("/401").excludePathPatterns("/404").excludePathPatterns("/500").excludePathPatterns("/register*");
        
        registry.addInterceptor(permssionInterceptor).addPathPatterns("/**").excludePathPatterns("/kaptch")
            .excludePathPatterns("/401").excludePathPatterns("/404").excludePathPatterns("/500")
            .excludePathPatterns("/").excludePathPatterns("/login*").excludePathPatterns("/home").excludePathPatterns("/register*");
        
        registry.addInterceptor(tablePermissionInterceptor).addPathPatterns("/**");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry)
    {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/**");
    }


    @Bean
    public EmbeddedServletContainerCustomizer containerCustomizer()
    {

        return (container -> {
              ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND,
              "/404"); ErrorPage error500Page = new
              ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/500");
              container.addErrorPages( error404Page, error500Page);
        });
    }

}
